package com.smart.manager.pop.repository;


import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjUtil;
import com.smart.manager.member.entities.models.MemberProfileMain;
import com.smart.manager.member.entities.models.query.QMemberProfileMain;
import com.smart.manager.pop.entities.assembler.ShareData;
import com.smart.manager.pop.entities.models.PopActivityMain;
import com.smart.manager.pop.entities.models.PopBasicLink;
import com.smart.manager.pop.entities.models.PopBasicPoster;
import com.smart.manager.pop.entities.models.PopC2cAttend;
import com.smart.manager.pop.entities.models.query.QPopActivityMain;
import com.smart.manager.pop.entities.models.query.QPopBasicLink;
import com.smart.manager.pop.entities.models.query.QPopC2cAttend;
import com.smart.manager.pop.entities.views.ActivityMainView;
import com.smart.service.poster.PosterService;
import io.ebean.DB;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

@Log4j2
@Service
@RequiredArgsConstructor
public class AsyncService {

private final PosterService posterService;

    @Async
    public void getActivityAttendUpdate( PopBasicPoster poster) {
        List<PopActivityMain> mains = new QPopActivityMain().posterId.eq(poster.getId()).findList();
        mains.forEach(activityRegister -> {
            Long activity = activityRegister.getId();
            ActivityMainView activityMainView = new ActivityMainView().setId(activity);
            List<PopC2cAttend> attendList = new QPopC2cAttend().activity.eq(activityMainView).findList();
            attendList.forEach(record -> {
                if (ObjUtil.isNotNull(record)) {
                    Long linkId = activityRegister.getLinkId();
                    PopBasicLink basicLink = new QPopBasicLink().id.eq(linkId).setMaxRows(1).findOne();
                    Assert.notNull(basicLink, "活动链接信息不存在");
                    String shareUrl = record.getShareData().getLink();
                    MemberProfileMain memberInfo = new QMemberProfileMain().id.eq(record.getMember().getId()).setMaxRows(1).findOne();
                    //生成海报
                    String posterUrl = posterService.makePosterImage(poster, shareUrl, memberInfo);
                    //获取分享数据
                    ShareData shareData = new ShareData().setLink(shareUrl).setPoster(posterUrl).setThumb(basicLink.getThumb()).setDescription(basicLink.getDescription()).setTitle(basicLink.getTitle());
                    //更新参与数据
                    record.setShareData(shareData);
                }
            });
            DB.updateAll(attendList);
        });
    }

    @Async
    public void getActivityAttendUpdate(PopActivityMain activityRegister) {
        Long activity = activityRegister.getId();
        ActivityMainView activityMainView = new ActivityMainView().setId(activity);
        List<PopC2cAttend> attendList = new QPopC2cAttend().activity.eq(activityMainView).findList();
        attendList.forEach(record->{
            if (ObjUtil.isNotNull(record)) {
                Long linkId = activityRegister.getLinkId();
                PopBasicLink basicLink = new QPopBasicLink().id.eq(linkId).setMaxRows(1).findOne();
                Assert.notNull(basicLink, "活动链接信息不存在");
                Long posterId = activityRegister.getPosterId();
                String shareUrl = record.getShareData().getLink();
                MemberProfileMain memberInfo = new QMemberProfileMain().id.eq(record.getMember().getId()).setMaxRows(1).findOne();
                //生成海报
                String posterUrl = posterService.makePosterImage(posterId, shareUrl, memberInfo);
                //获取分享数据
                ShareData shareData = new ShareData().setLink(shareUrl).setPoster(posterUrl).setThumb(basicLink.getThumb()).setDescription(basicLink.getDescription()).setTitle(basicLink.getTitle());
                //更新参与数据
                record.setShareData(shareData);
            }
        });
        DB.updateAll(attendList);
    }
}
